<?php

/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/6/20
 * Time: 下午2:29
 */
namespace app\index\exception;
use app\index\utils\ExceptionUtil;
use app\index\utils\ResultUtil;
use Exception;
use think\Controller;
use think\exception\Handle;
use think\exception\HttpException;
use think\facade\Validate;
use think\Request;
use think\Log;
use think\View;

class ExceptionHandler extends Handle
{
    private $code;
    private $msg;
    public function render(Exception $e)
    {
        /*
         * 该函数需要处理两种异常类型:
         * 一、是来自客户端的自定义错误类型,捕获来自BaseException
         * 二、是来自服务器自身的异常
         */


        //自定义异常处理
        if($e instanceof BaseException){
            $this->code = $e->code;
            $this->msg = $e->msg;
//            return ResultUtil::error(0,"as");
//            return json($this->msg);

            //

        }else{
            if(config('app_debug')){
                return parent::render($e);
            }else{
                $this->msg = '服务器内部错误';
                $this->error_code = 999;
            }

        }
        return ResultUtil::error($this->code,$this->msg);


        //RestFul API 统一的错误信息返回格式


    }




}